/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action;

import java.io.*;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import negocio.TicketManager;
import persistencia.pojo.Ticket;

/**
 *
 * @author Victor
 */
public class BuscarTicketReabrirAction extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String tipo_bus = request.getParameter("tipo_bus");
        String dato_bus = request.getParameter("dato_bus");
        String pag_actual = request.getParameter("pag_actual");  
        String strColTabla = null;
        if(tipo_bus.equals("T")){
            strColTabla="co_ticket";
        }else{
            mensaje(response, "-2", "Error obteniendo información.", null);
            return;
        }
        String[] arrDatosTicket = new String[8];
        try {
            
            TicketManager mgr = new  TicketManager();
            Ticket ticket = mgr.getTicket(dato_bus);
            
            arrDatosTicket[0]= ticket.getCodigo();
            arrDatosTicket[1]= ticket.getCodigo_cliente();
            arrDatosTicket[2]= ticket.getTipo_ticket();
            arrDatosTicket[3]= ticket.getDesc_ticket();
            arrDatosTicket[4]= ticket.getCo_categoria();
            arrDatosTicket[5]= ticket.getCo_subcategoria();
            arrDatosTicket[6]= ticket.getSolucion();
            arrDatosTicket[7]= ticket.getEstado();
            
            mensaje(response, "0", "Datos", arrDatosTicket);             
        
        }catch (Exception e) {
            e.printStackTrace();
        }
    } 
    
    private void mensaje(HttpServletResponse response, String cod,  String mensa, String[] arrDatos) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {

            out.println("{\"proceso\": { \"res\": [\"" + cod +"\"], \"msg\": [\"" + mensa +"\"], " +
                    "\"co_ticket\": [\"" + arrDatos[0] +"\"]," +
                    "\"cliente\": [\"" + arrDatos[1] +"\"]," +
                    "\"tipo_ticket\": [\"" + arrDatos[2] +"\"]," +
                    "\"desc_ticket\": [\"" + arrDatos[3] +"\"]," +
                    "\"categoria\": [\"" + arrDatos[4] +"\"]," +
                    "\"subcategoria\": [\"" + arrDatos[5] +"\"]," +
                    "\"solucion\": [\"" + arrDatos[6] +"\"]," +
                    "\"estado\": [\"" + arrDatos[7] +"\"]" +
                    "}}");
            out.flush();
        } finally { 
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
